#include "gamelogic.h"
#include "qipan.h"
#include <QDebug>

GameLogic::GameLogic(QObject *parent) : QObject(parent)
{
}

/*设置棋盘信息*/
void GameLogic::setQipanBoard(Role **board, int boardSize)
{
    m_board = board;
    m_boardSize = boardSize;
}

int GameLogic::evaluateMove(int row, int col, Role role)
{
    int score = 0;
    /*连子数 + 开放端*/
    /*四个方向*/
    const int directions[4][2] =
    {
        /*水平*/
        {1,0},
        /*垂直*/
        {0,1},
        /*左对角*/
        {1,1},
        /*右对角*/
        {1,-1}
    };
    /*四个方向探测*/
    for(int direct = 0;direct < 4;direct++)
    {
        // 连子数
        int count = 0;
        // 开放端
        int openEnds = 0;
        /*向一个方向检查最多四个棋子*/
        for(int idx = 1;idx <= 4;idx++ )
        {
            int newRow = row + directions[direct][0] * idx;
            int newCol = col + directions[direct][1] * idx;
            /*检测新坐标是否在棋盘里面*/
            if(newRow >= 0 && newRow <m_boardSize &&
                    newCol >= 0 && newCol < m_boardSize)
            {
                // 是你
                if(m_board[newRow][newCol] == role)
                {
                    count++;
                }
                // 为空
                else if(m_board[newRow][newCol] == EMPTY)
                {
                    openEnds++;
                    break;
                }
                // 是对手
                else
                {
                    /*遇到对手的棋子*/
                    break;
                }
            }
            /*反方向*/
            /*向一个方向检查最多四个棋子*/
            for(int idx = 1;idx <= 4;idx++ )
            {

                int newRow = row - directions[direct][0] * idx;
                int newCol = col - directions[direct][1] * idx;
                /*检测新坐标是否在棋盘里面*/
                if(newRow >= 0 && newRow <m_boardSize &&
                        newCol >= 0 && newCol < m_boardSize)
                {
                    // 是你
                    if(m_board[newRow][newCol] == role)
                    {
                        count++;
                    }
                    // 为空
                    else if(m_board[newRow][newCol] == EMPTY)
                    {
                        openEnds++;
                        break;
                    }
                    // 是对手
                    else
                    {
                        /*遇到对手的棋子*/
                        break;
                    }
                }
            }
        }
        if(count == 4 && openEnds > 0)
        {
            score += 10000;
        }
        else if(count == 3 && openEnds > 0)
        {
            score += 1000;
        }
        else if(count == 2 && openEnds > 0)
        {
            score += 100;
        }
        else if(count == 1 && openEnds > 0)
        {
            score += 10;
        }
    }

    return score;
}

/*人机移动*/
void GameLogic::computerMove()
{
    qDebug()<<"选择下在哪个位置呢"<<endl;
    int bestScore = 0;
    int bestRow = -1;
    int bestCol = -1;
    int boardSize = m_boardSize;
    for(int row = 0;row <boardSize;row++)
    {
        for(int col = 0;col <boardSize;col++)
        {
            if(m_board[row][col] == 0)
            {
                /*判断这个点AI是不是最有利的*/
                /*1.对AI最有利 2.对对手最不利[对对手来说是最有利的]*/
                int score = evaluateMove(row,col,COMPUTER) + evaluateMove(row,col,PLAYER);
                if(bestScore < score)
                {
                    bestScore = score;
                    bestRow = row;
                    bestCol = col;
                }
            }
        }
    }
    qDebug()<<bestScore<<endl;
    emit AiPosition(bestRow,bestCol);
}

/*判断角色是否获胜*/
bool GameLogic::checkWin(int row, int col, Role role,int chessNum)
{
    /*四个方向*/
    const int directions[4][2] =
    {
        /*水平*/
        {1,0},
        /*垂直*/
        {0,1},
        /*左对角*/
        {1,1},
        /*右对角*/
        {1,-1}
    };

    /*四个方向探测*/
    for(int direct = 0;direct < 4;direct++)
    {
        /*初始化计数器*/
        int cnt = 1;
        /*向一个方向的正方向探测*/
        for(int idx = 1;idx < chessNum;idx++)
        {
            int newRow = row + directions[direct][0] * idx;
            int newCol = col + directions[direct][1] * idx;
            if(newRow >= 0 && newRow <m_boardSize &&
                    newCol >= 0 && newCol < m_boardSize &&
                    m_board[newRow][newCol] == role)
            {
                cnt++;
            }
            else
            {
                break;
            }
        }

        /*向一个方向的反方向探测*/
        for(int idx = 1;idx < chessNum;idx++)
        {
            int newRow = row - directions[direct][0] * idx;
            int newCol = col - directions[direct][1] * idx;
            if(newRow >= 0 && newRow < m_boardSize && newCol >= 0 && newCol < m_boardSize &&
                    m_board[newRow][newCol] == role)
            {
                cnt++;
            }
            else
            {
                break;
            }
        }
        if(cnt >= chessNum)
        {
            return true;
        }
    }
    return false;
}

//void GameLogic::algorithmAi(int &aiRow, int &aiCol)
//{
//    /* 优先取胜策略 */
//    for (int row = 0; row < m_boardSize; row++)
//    {
//        for (int col = 0; col < m_boardSize; col++)
//        {
//            if (m_board[row][col] == EMPTY)
//            {
//                m_board[row][col] = COMPUTER;
//                if (checkWin(row, col, COMPUTER, 5))
//                {  // AI 获胜
//                    aiRow = row;
//                    aiCol = col;
//                    return;
//                }
//                m_board[row][col] = EMPTY;
//            }
//        }
//    }

//    /* 阻止对手策略 */
//    for (int row = 0; row < m_boardSize; row++)
//    {
//        for (int col = 0; col < m_boardSize; col++)
//        {
//            if (m_board[row][col] == EMPTY)
//            {
//                m_board[row][col] = PLAYER;
//                if (checkWin(row, col, PLAYER, 5))
//                {
//                    aiRow = row;
//                    aiCol = col;
//                    return;
//                }
//                m_board[row][col] = EMPTY;
//            }
//        }
//    }
//}
